package com.hualan.sfzyproject.service;

import com.hualan.sfzyproject.domain.ConsultationAppointment;
import com.hualan.sfzyproject.mapper.ConsultationAppointmentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class ConsultationAppointmentService {

    @Autowired
    private ConsultationAppointmentMapper consultationAppointmentMapper;

    public List<ConsultationAppointment> selectByUserId(int userId, int pageNum, int pageSize) {
        List<ConsultationAppointment> allConsultationAppointments = consultationAppointmentMapper.selectByUserId(userId);
        int maxPage = (int) Math.ceil((double) allConsultationAppointments.size() / pageSize);
        if (allConsultationAppointments.isEmpty() || pageNum > maxPage) {
            return null;
        }
        int start = Math.max(0, (pageNum - 1) * pageSize);
        int end = Math.min(allConsultationAppointments.size(), start + pageSize);
        return allConsultationAppointments.subList(start, end);
    }
    public int updateStatusByAppointmentId(@RequestParam("appointmentId") int appointmentId, @RequestParam("status") int status) {
        return consultationAppointmentMapper.updateStatusByAppointmentId(appointmentId, status);
    }

    public int insert(ConsultationAppointment consultationAppointment) {
        return consultationAppointmentMapper.insert(consultationAppointment);
    }

    public List<ConsultationAppointment> selectByConsultantId(int consultantId) {
        return consultationAppointmentMapper.selectByConsultantId(consultantId);
    }
}
